var mongoose = require('./mongoose.js').mongoose;
var CommentModel = require('../models/comment.js')(mongoose);
var default_config = require('../config/deault.js');
module.exports = {
	addComment:function (blog_id,user_id,user_name,comment,callback) {
		var cm = new CommentModel({
			blog_id:blog_id,
			user_id:user_id,
			comment:comment,
			user_name:user_name,
			add_time:Date.now()
		});
		cm.save(function (err,c) {
			if(err){
				callback(false,null);
			}else{
				callback(true,c);
			}
		});
	},
	getComments:function (pageIndex,blog_id,callback) {
		var limit = default_config.pageSize;
		var skip = (pageIndex - 1) * limit;
		CommentModel.find({
			blog_id:blog_id
		}).skip(skip).limit(limit).sort('-add_time').exec(function (err,comments) {
			if(err){
				callback(false,[]);
			}else {
				callback(true,comments);
			}
		});
	}
};